﻿using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SZ.Mech;
using UnityEngine;
using Verse;
using Verse.Noise;
namespace SZ.Mech
{
    [StaticConstructorOnStartup]
    public class Verb_CastAbilityDragonFlyMap : Verb_CastAbility
    {
        public static readonly Texture2D TargeterMouseAttachment = ContentFinder<Texture2D>.Get("UI/Abilities/SZ_DragonFlyUI");

        DragonFlyExtension dragonFlyExtension;

        protected override bool TryCastShot()
        {
            Find.Targeter.BeginTargeting(TargetingParameters.ForDropPodsDestination(), delegate (LocalTargetInfo x)
            {
                dragonFlyExtension = ability.def.GetModExtension<DragonFlyExtension>();
                var tile = caster.Map.Tile;
                var map = Find.WorldObjects.WorldObjectAt<MapParent>(tile);

                TryLaunch(tile, new TransportPodsArrivalAction_SZLandInSpecificCell(map, x.Cell, dragonFlyExtension.activeDropPodFly, dragonFlyExtension.dropPodIncomingFly));//这里会有个传送到达提示，但可以改
            }, null, null, TargeterMouseAttachment);
            ability.StartCooldown(ability.def.cooldownTicksRange.RandomInRange);
            return true;
        }

        public void TryLaunch(int destinationTile, TransportPodsArrivalAction arrivalAction)
        {
            Pawn p = caster as Pawn;
            if (p.drafter != null)
            {
                p.drafter.Drafted = false;
            }
            if (!this.caster.Spawned)
            {
                Log.Error(string.Concat("Tried to launch ", this.caster, ", but it's unspawned."));
                return;
            }
            Map map = caster.Map;
        
            //int num = Find.WorldGrid.TraversalDistanceBetween(map.Tile, destinationTile);
            ActiveDropPod activeDropPod = (ActiveDropPod)ThingMaker.MakeThing(dragonFlyExtension.activeDropPodFly);//行动吊舱                                                                                  //ActiveDropPod activeDropPod = (ActiveDropPod)ThingMaker.MakeThing(SZ_DefOf.SZ_DragonFlyA);//行动吊舱
            activeDropPod.Contents = new ActiveDropPodInfo();
            caster.DeSpawn(DestroyMode.Vanish);
            activeDropPod.Contents.innerContainer.TryAddOrTransfer(caster, true);

            //activeDropPod.Contents.innerContainer.TryAdd(caster,true);
            //activeDropPod.Contents.innerContainer.TryAddRangeOrTransfer(new List<Thing> { caster });

            FlyShipLeaving obj = (FlyShipLeaving)SkyfallerMaker.MakeSkyfaller(dragonFlyExtension.dropPodLeavingFly, activeDropPod);//飞行中的效果
                                                                                                                                   //FlyShipLeaving obj = (FlyShipLeaving)SkyfallerMaker.MakeSkyfaller(SZ_DefOf.DropPodLeavingDragonFlyA, activeDropPod);//飞行中的效果
            obj.groupID = Find.UniqueIDsManager.GetNextTransporterGroupID();//可能可以这样写
            obj.destinationTile = destinationTile;
            obj.arrivalAction = arrivalAction;//到达行动
            obj.worldObjectDef = WorldObjectDefOf.TravelingTransportPods;
            //compTransporter.CleanUpLoadingVars(map);
            //compTransporter.parent.Destroy();
            GenSpawn.Spawn(obj, caster.Position, map);
        }
    }
}

